package cc.fs.zhihu.ui.fragment

import android.app.Fragment
import android.os.Bundle
import android.provider.MediaStore
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import cc.fs.zhihu.R
import cc.fs.zhihu.beans.VideoBean1
import cc.fs.zhihu.ui.VideoActivity
import cc.fs.zhihu.ui.adapter.VideoAdapter
import cc.fs.zhihuapp.utils.bindView
import java.io.File
import java.util.*

/**
 * Created by fostion on 2015/10/13.
 */
class VideoFragment : Fragment() {

    val videos:ArrayList<VideoBean1> = ArrayList()
    val recycleview: RecyclerView by bindView(R.id.recycleview)
    var adapter:VideoAdapter ? = null


    override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater?.inflate(R.layout.fragment_video,container,false)
    }

    override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        adapter = VideoAdapter(videos)
        recycleview.layoutManager = LinearLayoutManager(activity)
        recycleview.adapter = adapter
        adapter?.onClick = {
            video -> VideoActivity.start(activity,video)
        }
    }

    fun setFile(path:String){
        videos.clear()
        val file = File(path)
        val files = file.listFiles()
        for(video in files){
            if(!video.isDirectory){
                if(isVideo(video.name)){
                    val videoBean = VideoBean1()
                    videoBean.title = video.name
                    videoBean.path = video.absolutePath
                    videoBean.size = video.totalSpace
                    videos.add(videoBean)
                }
            }
        }
        adapter?.notifyDataSetChanged()
    }

    fun isVideo(name:String):Boolean{
        if(name.endsWith(".mp4") || name.endsWith(".avi") || name.endsWith(".mov")|| name.endsWith(".asf")|| name.endsWith(".wmv")|| name.endsWith(".3gp")|| name.endsWith(".mkv")|| name.endsWith(".flv")){
            return true
        }
        return false
    }

    fun getVideo(){
        val cursor = activity.contentResolver.query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, null, null,null, null)

        cursor.moveToFirst()
        while (!cursor.isAfterLast) {
            val video = VideoBean1()
            video.id = ""+cursor.getInt(0)//cursor.getColumnIndexOrThrow(MediaStore.Video.VideoColumns.BUCKET_ID))
            video.title = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.TITLE))
            video.path = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA))
            video.mimeType = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.MIME_TYPE))
            video.duration = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Video.VideoColumns.DURATION))
            video.size = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.SIZE))
            //get thumb
            val section = MediaStore.Video.Thumbnails.VIDEO_ID +"=" + video.id
            val thumbCursor = activity.contentResolver.query(MediaStore.Video.Thumbnails.EXTERNAL_CONTENT_URI, null, section,null, null)
            if(thumbCursor.moveToFirst()){
                video.imgPath = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Thumbnails.DATA))
            }
            videos.add(video)
            cursor.moveToNext()
        }
    }

}